//----------------Sonic CD Time Stone Script------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Sonic Aliases
private alias object.state : SSSonic.State
private alias object.frame : SSSonic.Frame

// States
private alias 0 : TIMESTONE_FALLING
private alias 1 : TIMESTONE_HELD

// Special Stage Sonic States
private alias 10 : SSSONIC_STONEGRABBED

// SFX Aliases
private alias 21 : SFX_G_ACHIEVEMENT


event ObjectDraw
	object.draworder = 6
	// Get the frame the Stone should draw
	// (But note that it's not drawn till the end, see there)
	temp0 = object.frame
	temp0 /= 12

	// Animate the Stone
	object.frame++
	if object.frame > 47
		object.frame = 0
	end if

	if object.state == TIMESTONE_FALLING
		// Stone's still falling, so draw the sparkles that it's dropping

		// Smaller sparkle, to be 32 pixels above the Time Stone
		temp1 = object.ypos
		temp1 -= 0x200000
		DrawSpriteFX(5, FX_FLIP, object.xpos, temp1)

		// Bigger sparkle, 12 pixels below the smaller sparkle (absolute difference of 20 pixels above the Time Stone)
		temp1 += 0xC0000
		DrawSpriteFX(4, FX_FLIP, object.xpos, temp1)

		// Flip the object when needed in order to make the rotating animation appear smoother
		object.direction = object.frame
		object.direction %= 24
		object.direction /= 6

		if object.iypos < 200
			// Time Stone isn't at its target destination yet, keep it falling at a rate of 1.5 px per frame
			object.ypos += 0x18000
		else

			// Snap to Sonic's hands
			object.iypos = 200

			// Stop further movement of the Stone
			object.state = TIMESTONE_HELD
			PlaySfx(SFX_G_ACHIEVEMENT, false)

			// Signal to the Sonic Object that he's grabbed the Stone, from here he'll handle the rest
			SSSonic.State[2] = SSSONIC_STONEGRABBED
			SSSonic.Frame[2]++

#platform: Use_Origins
			// Let the Engine know we've touched an... "emerald"?
// WARNING: Function FUNC_ENGINECALLBACK does not exist in RSDKv4!
// Arguments: NOTIFY_TOUCH_EMERALD

#endplatform

		end if
	end if

	// Draw the actual Stone sprite
	// It's drawn all the way at the end of the process so that it stays above the sparkles, drawn before
	DrawSprite(temp0)

end event


event ObjectStartup

	LoadSpriteSheet("Special/Objects.gif")

	// 0-3 - Time Stone Frames
	SpriteFrame(-10, -12, 20, 24, 1, 232)
	SpriteFrame(-10, -12, 20, 24, 22, 232)
	SpriteFrame(-10, -12, 20, 24, 43, 232)
	SpriteFrame(-10, -12, 20, 24, 64, 232)

	// 4-5 - Sparkles
	SpriteFrame(-12, -12, 24, 24, 83, 143)
	SpriteFrame(-8, -8, 16, 16, 83, 126)

	// No extra sprites needed for alternate colours - they're handled via palettes stored in the StageConfig instead

end event


// ========================
// Editor Subs
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(-10, -12, 20, 24, 1, 232)


end event
